from rich import print
from alive_progress import alive_bar

from util.load_puzzles import load_from_file
from visualizer.board_visualizer import visualize_board
from visualizer.comp import visualize_transforms, visualize_comp, visualize_components
import visualizer.common as v
from util.prechecker import *
from visualizer.precheck_visualizer import *
from puzzle.board import Board
from matplotlib.patches import Polygon
from puzzle.mathF import *
from puzzle.tile import TileDesc

def draw_convex(vertices: list[Vec2]):
    fig, ax = plt.subplots()
    
    v.prepare_ax(ax)
    
    v.draw_polygon(
        ax,
        vertices,
        facecolor = (0.5, 0.5, 0.5),
        edgecolor = (0, 0, 0)
    )
    
    convex = get_convex_hull(vertices)
    v.draw_polygon(
        ax,
        convex,
        facecolor = None,
        edgecolor = (1, 0, 0),
        linewidth = 2
    )
    
    for i in range(len(convex)):
        vt = convex[i]
        ax.text(vt.x, vt.y, str(i), ha="center", va="center", fontsize = 16)
    
    ax.xaxis.set_major_locator(NullLocator())
    ax.yaxis.set_major_locator(NullLocator())
    plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) # https://blog.csdn.net/jifaley/article/details/79687000
    plt.margins(0, 0)
    
    v.show()

if __name__ == "__main__":
    board, _ = load_from_file("Test", "./data/des_apad.txt", validation = False)
    
    t: list[TileDesc] = [None] + [board.get_td_by_label(str(i)) for i in range(1, 32)] # type: ignore
    
    vertices = [
        [
            t[15].v_left,
            t[15].v_alone,
            t[16].v_right,
            t[18].v_right,
            t[18].v_alone,
            t[17].v_left,
        ],
        [
            t[16].v_left,
            t[16].v_right,
            t[7].v_alone,
            t[7].v_right,
            t[17].v_right,
            t[17].v_left,
        ],
        [
            t[17].v_left,
            t[17].v_alone,
            t[6].v_left,
            t[6].v_right,
            t[7].v_right,
            t[19].v_right,
            t[19].v_left
        ],
        [
            t[17].v_left,
            t[17].v_alone,
            t[7].v_alone,
            t[9].v_alone,
            t[9].v_right,
            t[9].v_left,
            t[18].v_alone
        ],
        [
            t[15].v_left,
            t[15].v_alone,
            t[16].v_right,
            t[18].v_right,
            t[19].v_right,
            t[17].v_right,
            t[17].v_left
        ],
        [
            t[5].v_left,
            t[5].v_alone,
            t[5].v_right,
            t[7].v_alone,
            t[7].v_right,
            t[17].v_right,
            t[17].v_left
        ],
        [
            t[15].v_left,
            t[5].v_left,
            t[5].v_alone,
            t[5].v_right,
            t[7].v_alone,
            t[7].v_right,
            t[17].v_right,
            t[17].v_left
        ],
        [
            t[3].v_left,
            t[3].v_alone,
            t[6].v_left,
            t[6].v_right,
            t[17].v_alone,
            t[17].v_right,
            t[17].v_left,
            t[16].v_left
        ],
        [
            t[15].v_left,
            t[5].v_left,
            t[5].v_alone,
            t[7].v_alone,
            t[18].v_right,
            t[19].v_right,
            t[17].v_right,
            t[17].v_alone,
            t[17].v_left
        ]
    ]
    
    area_comp = []
    area_convex = []
    rvalue = []
    
    for vt in vertices:
        a_comp = polygon_area(vt)
        a_convex = polygon_area(get_convex_hull(vt))
        
        area_comp.append(a_comp)
        area_convex.append(a_convex)
        rvalue.append(
            abs(a_convex / a_comp - 1)
        )
        
        # draw_convex(vt)
        # print()
        #     polygon_area(vt),
        #     polygon_area(get_convex_hull(vt)),
        #     "R =",
        #     round(polygon_area(get_convex_hull(vt)) / polygon_area(vt) - 1, 2)
        # )

    print(area_comp)
    print(area_convex)
    print(rvalue)
    
    print("R bar =", round(sum(rvalue) / len(rvalue), 4))
    print("VC =", round(np.var(area_comp), 4))